package cn.edu.nhic.nhcarsb.controller;

import cn.edu.nhic.nhcarsb.entity.Product;
import cn.edu.nhic.nhcarsb.entity.R;
import cn.edu.nhic.nhcarsb.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController extends BaseController{
    @Autowired
    private ProductService productService;
    @GetMapping
    public R list(){
        List<Product> productList = productService.list();
        return R.success(productList);
    }
    @GetMapping("/secKill")
    public R secKill(){
        //select * from product where psecKill=1 order by pdata desc
        /*QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("psecKill",1);*/
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Product::getPseckill,1).orderByDesc(Product::getPdate);
        List<Product> productList = productService.list(lambdaQueryWrapper);
        return R.success(productList);
    }
    @GetMapping("/home/{type}/{page}/{pageSize}")
    public R home(@PathVariable Integer type,@PathVariable Integer page,@PathVariable Integer pageSize){
        Page<Product> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (1==type){
            lambdaQueryWrapper.eq(Product::getPhot,1);
        }else if(2==type){
            lambdaQueryWrapper.eq(Product::getPnew,1);
        }else {
            lambdaQueryWrapper.eq(Product::getPgood,1);
        }
        lambdaQueryWrapper.orderByDesc(Product::getPdate);
        Page<Product> productPage = productService.page(pageInfo, lambdaQueryWrapper);
        return R.success(productPage);
    }
}
